package com.csu.tingtian.controller;

import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.entity.Fruit;
import com.csu.tingtian.entity.LegalFarm;
import com.csu.tingtian.entity.ProvideProduce;
import com.csu.tingtian.entity.Vegetable;
import com.csu.tingtian.service.ProduceService;
import com.csu.tingtian.vo.BusinessProduce;
import com.csu.tingtian.vo.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/produce/")
public class ProduceController {
    @Autowired
    private ProduceService produceService;


    //根据农产品的id，获取上架了这个产品的所有商家信息（农场名，商家用户名，产品价格，店铺评分，农场地址）
    @GetMapping("products/{id}/business")
    @ResponseBody
    public CommonResponse<List<Produce>> getProduceById(@PathVariable("id") String id) {
        return produceService.getProduceById(id);
    }

    //根据farmName获取该农场详细信息
    @GetMapping("farmname/{farmname}")
    @ResponseBody
    public CommonResponse<LegalFarm> getFarm(@PathVariable("farmname") String farmName) {
        return produceService.getFarm(farmName);
    }

    //根据fruitId获取该水果详细信息
    @GetMapping("fruits/{id}")
    @ResponseBody
    public CommonResponse<Fruit> getFruitById(@PathVariable("id") String id) {
        return produceService.getFruitById(id);
    }

    //根据vegetableId获取该蔬菜详细信息
    @GetMapping("vegetable/{id}")
    @ResponseBody
    public CommonResponse<Vegetable> getVegetableById(@PathVariable("id") String id) {
       return produceService.getVegetableById(id);
    }

    //获取所有水果
    @GetMapping("fruits")
    @ResponseBody
    public CommonResponse<List<Fruit>> getAllFruit() {
        return produceService.getAllFruit();
    }

    //获取所有蔬菜
    @GetMapping("vegetable")
    @ResponseBody
    public CommonResponse<List<Vegetable>> getAllVegetable() {
        return produceService.getAllVegetable();
    }

    //商家上架农产品，即插入一条记录到provideproduce表中
    @PostMapping("products/{id}/price/{price}/farmname/{farmname}")
    @ResponseBody
    CommonResponse<ProvideProduce> insertProvideProduce(@PathVariable("id") String id, @PathVariable("price") double price, @PathVariable("farmname") String farmName) {
        return produceService.insertProvideProduce(id,price,farmName);
    }

    //商家下架农产品，即删除provideproduce表中的某条记录
    @PostMapping("products/{id}/farmname/{farmname}")
    @ResponseBody
    CommonResponse<String> deleteProvideProduce(@PathVariable("id") String id,@PathVariable("farmname") String farmName) {
        return produceService.deleteProvideProduce(id,farmName);
    }

    //根据farmName获取他上架的所有农产品（provideProduce）
    @GetMapping("products/{farmname}")
    CommonResponse<List<BusinessProduce>> getBusinessAllProduce(@PathVariable("farmname") String farmName) {
        return produceService.getBusinessAllProduce(farmName);
    }
}
